import { fetchCargoListAPI, fetchOrderDetailAPI, fetchOrderListAPI } from '@/api/business/orderAPI'

const actions = {
  /*
  *
  * 获取订单分页的action
  *  */
  async fetchOrderListAction(context, pageParams) {
    const { data } = await fetchOrderListAPI(pageParams)
    context.commit('setOrderListMutation', data)
  },
  /*
  *
  * 获取订单详情的action
  *  */
  async fetchOrderDetailAction(context, orderId) {
    const { data } = await fetchOrderDetailAPI(orderId)
    context.commit('setOrderDetailMutation', data)
  },
  /*
  *
  * 获取货物列表的action
  *  */
  async fetchCargoListAction(context, orderId) {
    const { data } = await fetchCargoListAPI(orderId)
    context.commit('setCargoListMutation', data)
  }

}
const mutations = {
  /*
  *
  * 获取订单分页的mutation
  *  */
  setOrderListMutation(state, { counts, items }) {
    state.orderList = items
    state.counts = Number(counts)
  },
  /*
  * 设置订单详情的mutation
  *  */
  setOrderDetailMutation(state, orderDetail) {
    state.orderDetail = orderDetail
  },
  /*
  * 设置货物列表的mutation
  *  */
  setCargoListMutation(state, cargoList) {
    state.cargoList = cargoList
  }

}
const state = () => {
  return {
    orderList: [],
    counts: 0,
    orderDetail: {},
    cargoList: []
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
